package com.example.validatestudy.demos.dataStructures;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;

/**
 * @auther: 王振方
 * @date: 2024 03 02 18 03
 * @description:
 */
public class HashMapTest {

    public static void main(String[] args) {
        HashMap<Integer,String> map = new HashMap();
        map.put(1,"1");
        String result = map.get(1);
        /**
         * map的key可以为null
         */
        map.put(null,"1");
        System.out.println(JSON.toJSON(map));
        map.put(null,"2");
        System.out.println(JSON.toJSON(map));

        Hashtable<Integer,String> ht = new Hashtable();
        ht.put(1,"h");

        HashSet set = new HashSet();
        set.add(1);

        /**
         * 练习题 OneTest
         * 力扣第一题：两数之和
         */
        int[] arr = {2,4,5};
        int[] r =  one(arr,6);
        System.out.println(Arrays.toString(r));
    }

    public static int[] one(int[] arr,int target){
        HashMap<Integer,Integer> map = new HashMap();
        int[] result = new int[2];
        for (int i = 0; i < arr.length; i++) {
            int num = target - arr[i];
            if(map.containsKey(num)){
                result[0] = i;
                result[1] = map.get(num);
            }else{
                map.put(arr[i],i);
            }
        }
        return result;
    }

}
